<template>
	<view class="container">
		<view class="tui-searchbox">
			<view class="tui-search-input">
				<icon type="search" :size='13' color='#333'></icon>
				<input confirm-type="search" placeholder="苹果13" :focus="true" auto-focus placeholder-class="tui-input-plholder"
				 class="tui-input" v-model="key" @confirm="doSearch"/>
				<icon type="clear" :size='13' color='#bcbcbc' @click="cleanKey" v-show="key"></icon>
			</view>
			<view class="tui-cancle" @click="search">搜索</view>
		</view>

		<view v-show="searchKey.length==0" class="tui-search-history">
			<view class="tui-history-header">
				<view class="tui-search-title">搜索历史</view>
				<tui-icon name="delete" :size='14' color='#333' @click="openActionSheet" class="tui-icon-delete"></tui-icon>
			</view>
			<view class="tui-history-content">
				<block v-for="(item,index) in history" :key="index">
					<tui-tag margin="0 24rpx 24rpx 0" type="gray" shape="circle" @click="clickHistory(item)">{{item}}</tui-tag>
				</block>
			</view>
		</view>


		<!-- <view class="tui-search-hot">
			<view class="tui-hot-header">
				<view class="tui-search-title">大家正在搜</view>
			</view>
			<view class="tui-hot-content">
				<block v-for="(item,index) in hot" :key="index">
					<tui-tag type="gray" shape="circle">{{item}}</tui-tag>
				</block>
			</view>
		</view> -->
		<tui-actionsheet :show="showActionSheet" :tips="tips" @click="itemClick" @cancel="closeActionSheet"></tui-actionsheet>

		<view v-show="searchKey.length!=0">
			<!--screen-->
			<view class="tui-header-screen" :style="{ top: height + 'px' }">
				<view class="tui-screen-top">
					<view class="tui-top-item tui-icon-ml" :class="[tabIndex == 0 ? 'tui-active tui-bold' : '']" data-index="0" @click="screen">
						<view>{{ selectedName }}</view>
						<tui-icon :name="selectH > 0 ? 'arrowup' : 'arrowdown'" :size="14" :color="tabIndex == 0 ? '#e41f19' : '#444'"></tui-icon>
					</view>
					<view class="tui-top-item" @click="screen" :class="[tabIndex == 1 ? 'tui-active tui-bold' : '']" data-index="1">热度</view>
					<view class="tui-top-item" @click="screen" :class="[tabIndex == 2 ? 'tui-active tui-bold' : '']" data-index="2">
						<text>新品</text>
					</view>
					<view class="tui-top-item tui-icon-ml" @click="screen" :class="[tabIndex == 3 ? 'tui-active tui-bold' : '']" data-index="3">
						<text>销量</text>
					</view>

					<!--下拉选择列表--综合-->
					<view class="tui-dropdownlist" :class="[selectH > 0 ? 'tui-dropdownlist-show' : '']" :style="{ height: selectH + 'rpx' }">
						<view class="tui-dropdownlist-item tui-icon-middle" :class="[item.selected ? 'tui-bold' : '']" v-for="(item, index) in dropdownList"
						 :key="index" @tap.stop="dropdownItem" :data-index="index">
							<text class="tui-ml tui-middle">{{ item.name }}</text>
							<tui-icon name="check" :size="16" color="#e41f19" :bold="true" v-if="item.selected"></tui-icon>
						</view>
					</view>
					<view class="tui-dropdownlist-mask" :class="[selectH > 0 ? 'tui-mask-show' : '']" @tap.stop="hideDropdownList"></view>
					<!--下拉选择列表--综合-->
				</view>
			</view>
			<!--screen-->
			<!--list-->
			<view class="list">
				<u-waterfall v-model="list" ref="uWaterfall">
					<template v-slot:left="{leftList}">
						<view class="demo-warter" v-for="(item, index) in leftList" :key="index" @click="details(item)">
							<!-- 警告：微信小程序不支持嵌入lazyload组件，请自行如下使用image标签 -->
							<!-- #ifndef MP-WEIXIN -->
							<u-lazy-load threshold="-450" border-radius="10" :image="item.image" :index="index"></u-lazy-load>
							<!-- #endif -->
							<!-- #ifdef MP-WEIXIN -->
							<view class="demo-img-wrap">
								<image class="demo-image" :src="item.image" mode="widthFix"></image>
							</view>
							<!-- #endif -->
							<view class="demo-title">
								{{item.title}}
							</view>
							<view class="demo-price">
								{{item.price}}元
							</view>
							<view class="demo-tag">
								<view class="demo-tag-owner">
									自营
								</view>
								<view class="demo-tag-text">
									放心购
								</view>
							</view>
							<view class="demo-shop">
								{{item.shop}}
							</view>
							<!-- 微信小程序无效，因为它不支持在template中引入组件 -->
							<u-icon name="close-circle-fill" color="#fa3534" size="34" class="u-close" @click="remove(item.id)"></u-icon>
						</view>
					</template>
					<template v-slot:right="{rightList}">
						<view class="demo-warter" v-for="(item, index) in rightList" :key="index" @click="details(item)">
							<!-- #ifndef MP-WEIXIN -->
							<u-lazy-load threshold="-450" border-radius="10" :image="item.image" :index="index"></u-lazy-load>
							<!-- #endif -->
							<!-- #ifdef MP-WEIXIN -->
							<view class="demo-img-wrap">
								<image class="demo-image" :src="item.image" mode="widthFix"></image>
							</view>
							<!-- #endif -->
							<view class="demo-title">
								{{item.title}}
							</view>
							<view class="demo-price">
								{{item.price}}元
							</view>
							<view class="demo-tag">
								<view class="demo-tag-owner">
									自营
								</view>
								<view class="demo-tag-text">
									放心购
								</view>
							</view>
							<view class="demo-shop">
								{{item.shop}}
							</view>
							<!-- 微信小程序无效，因为它不支持在template中引入组件 -->
							<u-icon name="close-circle-fill" color="#fa3534" size="34" class="u-close" @click="remove(item.id)"></u-icon>
						</view>
					</template>
				</u-waterfall>
				<u-loadmore margin-top="50" v-show="loadmoreShow" bg-color="#fff" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
				<tui-no-data v-show="!loadmoreShow" :fixed="false" imgUrl="/static/img/toast/img_nodata.png">暂无数据</tui-no-data>
			</view>

			<!--list-->

		</view>
		<u-no-network tips="哎呀，网络信号丢失"></u-no-network>
		<failTimeout v-show="failTimeout"></failTimeout>
	</view>

</template>

<script>
	import PmsPortalProductController from '../../api/PmsPortalProductController.js'
	import Cookies from '../../utils/Cookies.js'
	const util = require("@/utils/util.js")
	export default {
		data() {
			return {
				failTimeout:false,
				history: [],
				hot: [
					"德利赫特",
					"托雷斯",
					"早安D站",
					"D站观点",
					"德利赫特",
					"美洲杯",
					"华为手机",
					"C罗",
					"自热火锅",
					"2019退役球星",
					"女神大会"
				],
				key: "",
				showActionSheet: false,
				tips: "确认清空搜索历史吗？",
				searchResult: ["按照展示的列表输入关键词看效果", "thorui", "2019退役球星", "搜索关键词高亮显示", "模拟搜索结果集", "开源不易，需要鼓励", "人人为我，我为人人"],
				searchList: [],
				searchKey: '', //实际搜索关键字
				dropdownList: [{
						name: '综合',
						selected: true,
						sort:null,
					},
					{
						name: '价格升序',
						selected: false,
						sort:3
					},
					{
						name: '价格降序',
						selected: false,
						sort:4
					}
				],
				selectedName: '综合',
				selectH: 0,
				scrollTop: 0,
				dropScreenShow: false,
				attrIndex: -1,
				dropScreenH: 0, //下拉筛选框距顶部距离
				isList: false, //是否以列表展示  | 列表或大图
				loadStatus: 'loadmore',
				list: [
				// 	{
				// 	price: 35,
				// 	title: '北国风光，千里冰封，万里雪飘',
				// 	shop: '李白杜甫白居易旗舰店',
				// 	image: 'http://pic.sc.chinaz.com/Files/pic/pic9/202002/zzpic23327_s.jpg',
				// },
				],
				index: 0,
				load: true,
				sort: null,
				loadmoreShow:true,
				tabIndex:0,//顶部筛选索引
			}
		},
		onLoad: function(options) {
			let obj = {};
			// #ifdef MP-WEIXIN
			obj = wx.getMenuButtonBoundingClientRect();
			// #endif
			// #ifdef MP-BAIDU
			obj = swan.getMenuButtonBoundingClientRect();
			// #endif
			// #ifdef MP-ALIPAY
			my.hideAddToDesktopMenu();
			// #endif
			uni.getSystemInfo({
				success: res => {
					this.width = obj.left || res.windowWidth;
					this.height = obj.top ? obj.top + obj.height + 8 : res.statusBarHeight + 44;
					this.inputTop = obj.top ? obj.top + (obj.height - 30) / 2 : res.statusBarHeight + 7;
					this.arrowTop = obj.top ? obj.top + (obj.height - 32) / 2 : res.statusBarHeight + 6;
					
					if(options.searchKey==null||options.searchKey==''||options.searchKey=='undefined'){
						this.searchKey='';	
					}else{
						this.searchKey =decodeURIComponent ( options.searchKey);
						this.key = decodeURIComponent (options.searchKey);
						this.search()
					}
					
					//略小，避免误差带来的影响
					this.dropScreenH = (this.height * 750) / res.windowWidth + 186;
					this.drawerH = res.windowHeight - uni.upx2px(100) - this.height;
				}
			});
			this.history=Cookies.getSearchRecords()
			
		
			
		},
		onReachBottom() {
			if (this.load) {
				this.loadStatus = 'loading';
				// 模拟数据加载
				setTimeout(() => {
					this.addRandomData();
					this.loadStatus = 'loadmore';
				}, 1000)
			}
		},
		methods: {
			back: function() {
				uni.navigateBack();
			},
			cleanKey: function() {
				this.key = ''
				this.searchKey = ''
				this.list=[];
				this.clear();
				this.history=Cookies.getSearchRecords()
			},
			closeActionSheet: function() {
				this.showActionSheet = false
			},
			openActionSheet: function() {
				this.showActionSheet = true;
				Cookies.delSearchRecords();
			},
			itemClick: function(e) {
				let index = e.index;
				if (index == 0) {
					this.showActionSheet = false;
					this.history = []
				}
			},
			inputKey: function(e) {

			},
			search() {
				if(this.key!=''&&this.key!=null){
					this.load = true;
					this.index=0;
					this.list=[];
					this.clear();
					this.searchKey = this.key
					this.addRandomData();
					this.history=Cookies.getSearchRecords()
					Cookies.setSearchRecords(this.searchKey)
				}
				
			},
			hideDropdownList: function() {
				this.selectH = 0;
			},
			btnCloseDrop() {
				this.scrollTop = 1;
				this.$nextTick(() => {
					this.scrollTop = 0;
				});
				this.dropScreenShow = false;
				this.attrIndex = -1;
			},
			screen: function(e) {
				let index = e.currentTarget.dataset.index;
				this.hideDropdownList();
				this.btnCloseDrop();
				if (index == 0) {
					this.showDropdownList();
				} else if (index == 1) {
					this.sort=0;
					this.search();
				} else if (index == 2) {
					this.isList = !this.isList;
					this.sort=1;
					this.search();
				} else if (index == 3) {
					this.drawer = true;
					this.sort=2;
					this.search();
				}
				this.tabIndex = index;
				
				
			},
			showDropdownList: function() {
				this.selectH = 246;
				this.tabIndex = 0;
			},
			dropdownItem: function(e) {
				let index = e.currentTarget.dataset.index;
				let arr = this.dropdownList;
				for (let i = 0; i < arr.length; i++) {
					if (i === index) {
						arr[i].selected = true;
						this.sort=arr[i].sort;
					} else {
						arr[i].selected = false;
					}
				}
				this.dropdownList = arr;
				this.selectedName = index == 0 ? '综合' : '价格';
				this.selectH = 0;
				this.search();
			},
			 addRandomData() {		
				if (this.searchKey != null && this.searchKey != '' && this.searchKey != 'undefined') {
					if (this.load) {
						this.index++;
						let searc={};
						if(this.sort!=null){
							searc={ keyword:this.searchKey, pageNum:this.index, pageSize:10, sort:this.sort }
						}else{
							searc={ keyword:this.searchKey, pageNum:this.index, pageSize:10}
						}
						PmsPortalProductController.search(searc).then(res => {
							if (res.data.code == 200) {
						
								res.data.data.list.forEach(e => {
									this.list.push({
										price: e.price,
										title: e.subTitle,
										shop: e.brandName,
										image: e.pic,
										id: e.id,
									})
								})
								if (res.data.data.totalPage <= this.index) {
									this.load = false
									this.loadStatus = 'nomore'
								}

								if(res.data.data.totalPage==0){
									this.loadmoreShow=false;
								
								}else{
									this.loadmoreShow=true;
								}
							}
						}).catch(err => {
							if(err.errMsg=='request:fail timeout'){
								this.failTimeout=true
							}
						})
					}
				}


			},
			remove(id) {	//删除list某一个
				this.$refs.uWaterfall.remove(id);
			},
			clear() {	//清空list
				this.$refs.uWaterfall.clear();
			},
			details(item){		//点击跳转详情页面
				uni.navigateTo({
					url: "/pages/goodsDetails/goodsDetails?id=" + item.id,
				});
			},
			clickHistory(item){		//点击搜索记录
				this.key=item;
				this.search();
			},
			doSearch(){		//回车事件
				this.search();
			}
		}
	}
</script>

<style scoped>
	page {
		color: #333;
		background: #fff;
	}

	.container {

		box-sizing: border-box;
	}

	.tui-searchbox {
		padding: 10rpx 30rpx 0rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		width: 100%;
		background: #fff;
		position: fixed;
		z-index: 99998;
		left: 0;
		top: 0;
		
	}

	.tui-search-input {
		width: 100%;
		height: 66rpx;
		border-radius: 35rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		background: #f2f2f2;
		display: flex;
		align-items: center;
		flex-wrap: nowrap;
	}

	.tui-input {
		flex: 1;
		color: #333;
		padding: 0 16rpx;
		font-size: 28rpx;
	}

	.tui-input-plholder {
		font-size: 28rpx;
		color: #b2b2b2;
	}

	.tui-cancle {
		color: #888;
		font-size: 28rpx;
		padding-left: 30rpx;
		flex-shrink: 0;
	}

	.tui-search-history {
		margin-top: 50rpx;
		padding: 20rpx;
	}

	.tui-history-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx 0;
	}

	.tui-history-content {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
	}

	.tui-icon-delete {
		padding: 10rpx;
	}

	.tui-search-title {
		font-size: 28rpx;
		font-weight: bold;
	}

	.tui-hot-header {
		padding: 30rpx 0;
	}

	.tui-header {
		padding: 26rpx 0;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.tui-header-left {
		font-size: 30rpx;
		color: #222;
		border-left: 4rpx solid #EB0909;
		padding-left: 10rpx;
		word-break: break-all;
	}

	.tui-noboredr {
		border-left: 0 !important;
	}

	.tui-result-box {
		font-size: 28rpx;
	}

	.tui-result-item {
		line-height: 28rpx;
		padding: 28rpx 0;
	}
</style>

<style lang="scss" scoped>
	/* 隐藏scroll-view滚动条*/

	::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
	}


	/*screen*/

	.tui-header-screen {
		width: 100%;
		box-sizing: border-box;
		background: #fff;
		position: fixed;
		z-index: 1000;
		top: 70rpx;
		left: 0;
	}

	.tui-screen-top,
	.tui-screen-bottom {
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28rpx;
		color: #333;
	}

	.tui-screen-top {
		height: 88rpx;
		position: relative;
		background: #fff;
	}

	.tui-top-item {
		height: 28rpx;
		line-height: 28rpx;
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.tui-topitem-active {
		color: #e41f19;
	}

	.tui-screen-bottom {
		height: 100rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		font-size: 24rpx;
		align-items: center;
		overflow: hidden;
	}

	.tui-bottom-text {
		line-height: 26rpx;
		max-width: 82%;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.tui-bottom-item {
		flex: 1;
		width: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0 12rpx;
		box-sizing: border-box;
		background-color: #f7f7f7;
		margin-right: 20rpx;
		white-space: nowrap;
		height: 60rpx;
		border-radius: 40rpx;
	}

	.tui-bottom-item:last-child {
		margin-right: 0;
	}

	.tui-btmItem-active {
		background-color: #fcedea !important;
		color: #e41f19;
		font-weight: bold;
		position: relative;
	}

	.tui-btmItem-active::after {
		content: '';
		position: absolute;
		border: 1rpx solid #e41f19;
		width: 100%;
		height: 100%;
		border-radius: 40rpx;
		left: 0;
		top: 0;
	}

	.tui-btmItem-tap {
		position: relative;
		border-bottom-left-radius: 0;
		border-bottom-right-radius: 0;
	}

	.tui-btmItem-tap::after {
		content: '';
		position: absolute;
		width: 100%;
		height: 22rpx;
		background: #f7f7f7;
		left: 0;
		top: 58rpx;
	}

	.tui-bold {
		font-weight: bold;
	}

	.tui-active {
		color: #e41f19;
	}

	.tui-addr-left {
		padding-left: 6rpx;
	}

	.tui-seizeaseat-20 {
		height: 20rpx;
	}

	.tui-seizeaseat-30 {
		height: 30rpx;
	}

	/*screen*/


	/*顶部下拉选择 综合*/

	.tui-dropdownlist {
		width: 100%;
		position: absolute;
		background-color: #fff;
		border-bottom-left-radius: 24rpx;
		border-bottom-right-radius: 24rpx;
		overflow: hidden;
		box-sizing: border-box;
		padding-top: 10rpx;
		padding-bottom: 26rpx;
		left: 0;
		top: 88rpx;
		visibility: hidden;
		transition: all 0.2s ease-in-out;
		z-index: 999;
	}

	.tui-dropdownlist-show {
		visibility: visible;
	}

	.tui-dropdownlist-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.6);
		z-index: -1;
		transition: all 0.2s ease-in-out;
		opacity: 0;
		visibility: hidden;
	}

	.tui-mask-show {
		opacity: 1;
		visibility: visible;
	}

	.tui-dropdownlist-item {
		color: #333;
		height: 70rpx;
		font-size: 28rpx;
		padding: 0 40rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	/*顶部下拉选择 综合*/

	.tui-drawer-box {
		width: 686rpx;
		font-size: 24rpx;
		overflow: hidden;
		position: relative;
		padding-bottom: 100rpx;
	}

	.tui-drawer-title {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		box-sizing: border-box;
		height: 80rpx;
	}

	.tui-title-bold {
		font-size: 26rpx;
		font-weight: bold;
		flex-shrink: 0;
	}

	.tui-location {
		margin-right: 6rpx;
	}

	.tui-attr-right {
		width: 70%;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		text-align: right;
	}

	.tui-all-box {
		width: 90%;
		white-space: nowrap;
		display: flex;
		align-items: center;
		justify-content: flex-end;
	}

	.tui-drawer-content {
		padding: 16rpx 30rpx 30rpx 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
	}



	.tui-phcolor {
		text-align: center;
		color: #b2b2b2;
		font-size: 24rpx;
	}

	.tui-flex-attr {
		flex-wrap: wrap;
		justify-content: flex-start;
	}

	.tui-attr-item {
		width: 30%;
		height: 64rpx;
		background-color: #f7f7f7;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0 4rpx;
		box-sizing: border-box;
		border-radius: 32rpx;
		margin-right: 5%;
		margin-bottom: 5%;
	}

	.tui-attr-ellipsis {
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
		width: 96%;
		text-align: center;
	}

	.tui-attr-item:nth-of-type(3n) {
		margin-right: 0%;
	}

	.tui-attr-btnbox {
		width: 100%;
		position: absolute;
		left: 0;
		bottom: 0;
		box-sizing: border-box;
		padding: 0 30rpx;
		background: #fff;
	}

	.tui-attr-safearea {
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-bottom: env(safe-area-inset-bottom);
	}

	.tui-safearea-bottom {
		width: 100%;
		height: env(safe-area-inset-bottom);
	}

	.tui-attr-btnbox::before {
		content: '';
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		border-top: 1px solid #eaeef1;
		transform: scaleY(0.5) translateZ(0);
		transform-origin: 0 0;
	}

	.tui-drawer-btn {
		width: 47%;
		text-align: center;
		height: 60rpx;
		border-radius: 30rpx;
		flex-shrink: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		box-sizing: border-box;
	}

	.tui-drawerbtn-black {
		border: 1rpx solid #555;
	}

	.tui-drawerbtn-primary {
		background: #e41f19;
		color: #fff;
	}

	/* 商品列表*/

	.list {
		margin-top: 140rpx;
		margin-bottom: 70rpx;
	}

	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}

	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.demo-image {
		width: 100%;
		border-radius: 4px;
	}

	.demo-title {
		font-size: 26rpx;
		margin-top: 5px;
		color: $u-main-color;
		overflow: hidden;
		word-break: break-all;
		/* break-all(允许在单词内换行。) */
		text-overflow: ellipsis;
		/* 超出部分省略号 */
		display: -webkit-box;
		/** 对象作为伸缩盒子模型显示 **/
		-webkit-box-orient: vertical;
		/** 设置或检索伸缩盒对象的子元素的排列方式 **/
		-webkit-line-clamp: 2;
		/** 显示的行数 **/
	}

	.demo-tag {
		display: flex;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}

	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}

	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}

	/* 商品列表*/
</style>
<style>
	.list .u-divider {
		background-color: rgb(240, 240, 240) !important;
	}
</style>
